package com.wing.product.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseProcessingReport;
import com.wing.product.model.request.WarehouseProcessingReportForm;
import com.wing.product.model.response.WarehouseProcessingReportVO;
import com.wing.product.model.response.WarehouseProductionOrderVO;
import com.wing.product.service.WarehouseProcessingReportService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "加工报工管理", tags = "#/warehouse/product/warehouseProcessingReport")
@RestController("WarehouseProcessingReportController")
@RequestMapping("/warehouse/processingReport")
@RequiredArgsConstructor
public class WarehouseProcessingReportController {

    private final WarehouseProcessingReportService warehouseProcessingReportService;


    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProcessingReportVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String productionPlanCode,
            String status,
            String finishedProductBundleCode,
            String computerPracticeCode,
            Long productionLineId,
            Long productVarietyId,
            Long productMarkId,
            Long productProducerId,
            String code,
            String customerName,
            Long customerId
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseProcessingReport> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("productionPlanCode", productionPlanCode);
        paramMap.put("finishedProductBundleCode", finishedProductBundleCode);
        paramMap.put("computerPracticeCode", computerPracticeCode);
        paramMap.put("customerId", customerId);
        paramMap.put("productionLineId", productionLineId);
        paramMap.put("customerName", customerName);
        paramMap.put("productVarietyId", productVarietyId);
        paramMap.put("productMarkId", productMarkId);
        paramMap.put("productProducerId", productProducerId);
        paramMap.put("code", code);
        paramMap.put("status", status);

        //搜索
        IPage<WarehouseProcessingReportVO> list = warehouseProcessingReportService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询报工详情", notes = "查询")
    @GetMapping ("/selectProcessingReport")
    public JsonResult<WarehouseProcessingReportVO> selectProcessingReport(@RequestParam String id){
        if (!StringUtils.isEmpty(id)){
            WarehouseProcessingReportVO warehouseProcessingReportVO = warehouseProcessingReportService.selectProcessingReport(id);
            if (ObjectUtil.isNotEmpty(warehouseProcessingReportVO)){
                return  JsonResult.success("请求成功",warehouseProcessingReportVO);
            }else {
                return  JsonResult.success("数据为空",warehouseProcessingReportVO);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "添加报工记录", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseProcessingReportForm warehouseProcessingReportForm)  {
        try{
            return warehouseProcessingReportService.saveWarehouseProcessingReport(warehouseProcessingReportForm);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "修改报工记录", notes = "修改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/updateProcessingReport")
    public JsonResult updateProcessingReport(@RequestBody WarehouseProcessingReportForm warehouseProcessingReportForm)  {
        try{
            return warehouseProcessingReportService.updateProcessingReport(warehouseProcessingReportForm);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }
}
